package com.tongzhan.controller;

import com.tongzhan.base.Return;
import com.tongzhan.entity.party.PeopleOpinion;

import com.tongzhan.param.OpinionParam;
import com.tongzhan.service.OpinionService;
import com.tongzhan.utils.SnowFlakeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 社情民意
 *
 *
 * @date 2020/10/19
 */
@RequestMapping("/people")
@RestController
public class OpinionController {
    @Autowired
    private OpinionService opinionService;

    /**
     * 列表
     *
     * @param page
     * @param size
     * @param year_and_month
     * @param userId 用户id
     * @return
     */
    @GetMapping("/opinionList")
    public Return list(Integer page, Integer size, String year_and_month, String userId) {
        page = page == null ? 0 : page - 1;
        Map map = opinionService.getList(year_and_month, page, size, userId);
        return Return.getSuccessResult(map);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("/opinionDetail")
    public Return getDetail(String id,String userId) {
        if (StringUtils.isEmpty(id)) {
            return Return.getErrorResult("缺少id参数!");
        }
        PeopleOpinion detail = opinionService.getDetail(id,userId);
        return Return.getSuccessResult(detail);
    }


    /**
     * 添加
     *
     * @param saveModel
     * @return
     */
    @PostMapping("/addOpinion")
    public Return add(OpinionParam saveModel) {
        if (saveModel == null || saveModel.getConditions_content() == null || saveModel.getTitle() == null) {
            return Return.getErrorResult("参数为空");
        }
        if (saveModel.getTitle().length() > 50) {
            return Return.getErrorResult("标题过长");
        }
        if (saveModel.getConditions_content().length() > 500) {
            return Return.getErrorResult("内容不能为空");
        }
        if (StringUtils.isEmpty(saveModel.getUrl())) {
            return Return.getErrorResult("请上传图片");
        }

        PeopleOpinion entity = new PeopleOpinion();
        BeanUtils.copyProperties(saveModel, entity);
        entity.setId(new SnowFlakeUtils(3, 3).nextId() + "");
        entity.setAddUserId(saveModel.getUserId());
        long l = opinionService.add(entity);
        if (l > 0) {
            return Return.getSuccessResult("新增成功!");
        }
        return Return.getErrorResult("新增失败!");
    }
}
